package wangjg.mda.model.obj;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ManyToMany implements Cloneable {
	private String relation;				//UserRole
	
	private String model;					//User (this)
	private String key;						//id
	private String relationKey;				//UserRole.userId
	
	private String anotherModel;			//Role
	private String anotherKey;				//Role.id
	private String anotherRelationKey;		//UserRole.roleId
	
	private String anotherLabel;
	
	public ManyToMany() {}
	
	//User#id=UserRole#userId~UserRole#roleId=Role#id
	public static ManyToMany parse(String str){
		if(str==null||str.isEmpty()){
			return null;
		}
		String V = "[a-z_A-Z\\$\\.]\\w*";
		String ID = "("+V+")";
		String regex = ID+"#"+ID+"\\s*=\\s*"+ID+"#"+ID+"\\s*~\\s*"+ID+"#"+ID+"="+ID+"#"+ID+"(?:#"+ID+")?";
		Pattern pattern=Pattern.compile(regex, Pattern.MULTILINE|Pattern.DOTALL);
		Matcher matcher = pattern.matcher(str);
		if(!matcher.matches()){
			return parse2(str);
		}
		
		ManyToMany mtm = new ManyToMany();
		
		mtm.setRelation(matcher.group(3));
		
		mtm.setModel(matcher.group(1));
		mtm.setKey(matcher.group(2));
		mtm.setRelationKey(matcher.group(4));
		
		mtm.setAnotherModel(matcher.group(7));
		mtm.setAnotherKey(matcher.group(8));
		mtm.setAnotherRelationKey(matcher.group(6));
		mtm.setAnotherLabel(matcher.group(9));
		
		return mtm;
	}
	
	public ManyToMany clone(){
        try {
			return (ManyToMany) super.clone();
		} catch (CloneNotSupportedException e) {
		}
        return null;
	};
	
	//User.id=UserRole.userId~UserRole.roleId=Role.id#roleName
	public static ManyToMany parse2(String str){
		String V = "[a-z_A-Z\\$\\.]\\w*";
		String OBJ = "((?:"+V+"\\.)*"+V+")";
		String PP = "("+V+")";
		String regex = OBJ+"\\."+PP+"\\s*=\\s*"+OBJ+"\\."+PP+"\\s*~\\s*"+OBJ+"\\."+PP+"="+OBJ+"\\."+PP+"(?:#"+PP+")?";
		Pattern pattern=Pattern.compile(regex, Pattern.MULTILINE|Pattern.DOTALL);
		Matcher matcher = pattern.matcher(str);
		if(!matcher.matches()){
			return null;
		}
		
		ManyToMany mtm = new ManyToMany();
		
		mtm.setRelation(matcher.group(3));
		
		mtm.setModel(matcher.group(1));
		mtm.setKey(matcher.group(2));
		mtm.setRelationKey(matcher.group(4));
		
		mtm.setAnotherModel(matcher.group(7));
		mtm.setAnotherKey(matcher.group(8));
		mtm.setAnotherRelationKey(matcher.group(6));
		mtm.setAnotherLabel(matcher.group(9));
		
		return mtm;
	}
	/*
	 * 将两个model的关系对调
	 */
	public void switchModel(){
		String tmp = this.model;
		this.model = this.anotherModel;
		this.anotherModel = tmp;
		
		tmp = this.key;
		this.key = this.anotherKey;
		this.anotherKey = tmp;
		
		tmp = this.relationKey;
		this.relationKey = this.anotherRelationKey;
		this.anotherRelationKey = tmp;
	}

	public static void main(String args[]){
		ManyToMany mtm = ManyToMany.parse("User#id=UserRole#userId~UserRole#roleId=Role#id#roleName");
		System.out.println(mtm);
		mtm = ManyToMany.parse2("User.id=UserRole.userId~UserRole.roleId=xx.Role.id#roleName");
		System.out.println(mtm);
	}

	public String getRelation() {
		return relation;
	}

	public void setRelation(String relation) {
		this.relation = relation;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getRelationKey() {
		return relationKey;
	}

	public void setRelationKey(String relationKey) {
		this.relationKey = relationKey;
	}

	public String getAnotherModel() {
		return anotherModel;
	}

	public void setAnotherModel(String anotherModel) {
		this.anotherModel = anotherModel;
	}

	public String getAnotherKey() {
		return anotherKey;
	}

	public void setAnotherKey(String another_key) {
		this.anotherKey = another_key;
	}

	public String getAnotherRelationKey() {
		return anotherRelationKey;
	}

	public void setAnotherRelationKey(String anotherRelationKey) {
		this.anotherRelationKey = anotherRelationKey;
	}

	public String getAnotherLabel() {
		return anotherLabel;
	}

	public void setAnotherLabel(String anotherLabel) {
		this.anotherLabel = anotherLabel;
	}

	@Override
	public String toString() {
		return "ManyToMany [relation=" + relation + ", model=" + model
				+ ", key=" + key + ", relationKey=" + relationKey
				+ ", anotherModel=" + anotherModel + ", anotherKey="
				+ anotherKey + ", anotherRelationKey=" + anotherRelationKey
				+ ", anotherLabel=" + anotherLabel + "]";
	}
}
